package runner

import (
	"errors"
	"gitee.com/dreamwood/ez"
	"os"
	"os/exec"
	"syscall"
)

type Runner struct {
	SrcDir string   `yaml:"SrcDir"`
	Show   bool     `yaml:"Show"`
	Cmd    []string `yaml:"Cmd"`
}

func (this *Runner) Run() {
	e := os.Chdir(this.SrcDir)
	if ez.PrintError(e) {
		return
	}
	if len(this.Cmd) == 0 {
		ez.PrintError(errors.New("运行命令为空"))
		return
	}
	var cmd *exec.Cmd
	switch len(this.Cmd) {
	case 0:
		ez.PrintError(errors.New("运行命令为空"))
		return
	case 1:
		cmd = exec.Command(this.Cmd[0])
	default:
		cmd = exec.Command(this.Cmd[0], this.Cmd[1:]...)
	}
	if !this.Show {
		cmd.SysProcAttr = &syscall.SysProcAttr{CreationFlags: 0x08000000}
	}
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr
	e = cmd.Start()
	if ez.PrintError(e) {
		return
	}
}
